# Copyright (c) 2017 Presto Labs Pte. Ltd.
# Author: jaewon
# NOTE: Imported and modified from https://github.com/cyberdelia/atomic

import unittest

from coin.base.atomic import AtomicLong


class AtomicLongTest(unittest.TestCase):
  def test_init(self):
    atomic = AtomicLong()
    self.assertEqual(0, atomic.value)

    atomic = AtomicLong(0)
    self.assertEqual(0, atomic.value)

  def test_value(self):
    atomic = AtomicLong(0)
    atomic.value = 1
    self.assertEqual(1, atomic.value)

  def test_swap(self):
    atomic = AtomicLong(1000)
    swapped = atomic.swap(1001)
    self.assertEqual(1001, atomic.value)
    self.assertEqual(1000, swapped)

  def test_compare_and_set(self):
    atomic = AtomicLong(1000)
    self.assertTrue(atomic.compare_and_set(1000, 1001))
    self.assertEqual(1001, atomic.value)
    self.assertFalse(atomic.compare_and_set(1000, 1024))
    self.assertEqual(1001, atomic.value)

  def test_add(self):
    atomic = AtomicLong(1000)
    atomic += 1
    self.assertEqual(1001, atomic.value)
    self.assertEqual(1001, atomic.get_and_add(1))
    self.assertEqual(1002, atomic.value)

  def test_sub(self):
    atomic = AtomicLong(1000)
    atomic -= 1
    self.assertEqual(999, atomic.value)
    self.assertEqual(999, atomic.get_and_sub(1))
    self.assertEqual(998, atomic.value)


if __name__ == "__main__":
  unittest.main()
